package com.aoxon.cargo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.bean.GsonBean;
import com.aoxon.cargo.component.MyCallBack;
import com.aoxon.cargo.component.MyProgressDialog;
import com.aoxon.cargo.component.MyTextWatcher;
import com.aoxon.cargo.loader.NormalDataLoader;
import com.aoxon.cargo.service.MyServiceFactory;
import com.aoxon.cargo.service.SuggestionService;
import com.aoxon.cargo.state.MyStateUtil;
import com.aoxon.cargo.util.CheckStateUtil;
import com.aoxon.cargo.util.ToastUtil;

/**
 * 供应端问题反馈页面
 * 
 * @author ZZJ
 * 
 */
public class PurSuggestionActivity extends BaseActivity implements
		NormalDataLoader.OnLoadDataListener {

	private ImageView ivReturnButton;
	private ImageView ivCommitButton;

	private EditText etContent;

	private TextView tvTitle;

	private MyProgressDialog myProgressDialog;
	private MyTextWatcher myTextWatcher;

	private GsonBean gsonBean;
	private NormalDataLoader normalDataLoader = new NormalDataLoader(this);

	private SuggestionService suggestionService = (SuggestionService) MyServiceFactory
			.getInstance(MyServiceFactory.SUGGESTION);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setup();
	}


	private void setup() {

		setContentView(R.layout.pur_suggestion_feedback);

		etContent = (EditText) findViewById(R.id.etSuggestionText);

		ivReturnButton = (ImageView) findViewById(R.id.ivPublicTitleReturn);
		ivCommitButton = (ImageView) findViewById(R.id.ivPublicTitleOther);

		tvTitle = (TextView) findViewById(R.id.tvPublicTiTle);

		tvTitle.setText("问题反馈");
		ivCommitButton.setImageResource(R.drawable.button_public_commit);
		ivCommitButton.setEnabled(false);

		myProgressDialog = new MyProgressDialog(this);
		myTextWatcher = new MyTextWatcher(myCallBack);

		normalDataLoader.setOnloadDataListener(this);
		
		etContent.addTextChangedListener(myTextWatcher);
		ivReturnButton.setOnClickListener(myClickListener);
		ivCommitButton.setOnClickListener(myClickListener);
	}

	/**
	 * 数据提交结果的处理
	 */
	private void commitResult() {

		try {
			if (CheckStateUtil.check(gsonBean)) {

				CargoActivity.OPEN_CHOOSE = MyStateUtil.OPEN_MORE;
				Intent intent = new Intent(getBaseContext(),
						CargoActivity.class);
				startActivity(intent);
				this.finish();

				ToastUtil.show(getBaseContext(), "资料提交成功!");
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		ToastUtil.show(getBaseContext(), "资料提交失败，请重新提交！");

	}

	MyCallBack myCallBack = new MyCallBack() {
		@Override
		public void callback(String choose) {
			// TODO Auto-generated method stub
			if (etContent.getText().toString() != null
					&& !etContent.getText().toString().equals("")) {
				ivCommitButton.setEnabled(true);
			} else {
				ivCommitButton.setEnabled(false);
			}
		}
	};

	OnClickListener myClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.ivPublicTitleReturn:
				CargoActivity.OPEN_CHOOSE = MyStateUtil.OPEN_MORE;
				Intent intent = new Intent(getBaseContext(),
						CargoActivity.class);
				startActivity(intent);
				PurSuggestionActivity.this.finish();
				break;
			case R.id.ivPublicTitleOther:
				normalDataLoader.load();
				break;
			default:
				break;
			}
		}
	};

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			CargoActivity.OPEN_CHOOSE = MyStateUtil.OPEN_MORE;
			Intent intent = new Intent(getBaseContext(), CargoActivity.class);
			startActivity(intent);
			this.finish();
			return false;
		}
		return false;
	}

	@Override
	public void beforeLoad() {
		// TODO Auto-generated method stub
		myProgressDialog.show("提示", "提交中，请稍候...");
	}

	@Override
	public void toLoad(Message msg) throws Exception {
		// TODO Auto-generated method stub
		String content = etContent.getText().toString();
		gsonBean = suggestionService.execute(content);
		msg.what = MyStateUtil.UPLOAD_RESULT;
	}

	@Override
	public void afterLoad(int state) {
		// TODO Auto-generated method stub
		switch (state) {
		case MyStateUtil.UPLOAD_RESULT:
			commitResult();
			break;
		case MyStateUtil.RETURN_FAIL:
			ToastUtil.show(getBaseContext(), "无对应数据");
			break;
		case MyStateUtil.NOT_NET_CONNECTION:
			ToastUtil.show(getBaseContext(), "无网络连接");
			break;
		case MyStateUtil.ERROR_NET_CONNECTION:
			ToastUtil.show(getBaseContext(), "网络连接异常");
			break;
		default:
			break;
		}
	}

}
